package com.luo.produce.service;

import com.luo.common.base.RocketMQBuilder;
import com.luo.common.order.OrderCreateEvent;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 订单服务.
 *
 * @author Luo
 * @version 3.8
 * @date 2021-08-11 10:24
 */
@Slf4j
@Service
public class OrderService {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Value(value = "${rocketmq.order.topic}")
    private String orderTopic;

    /**
     * 推送订单创建消息.
     *
     * @param event 事件
     */
    public void sendOrderCreate(OrderCreateEvent event) {
        event.keys("123456789");

        orderTopic = orderTopic + ":" + OrderCreateEvent.class.getSimpleName();
        SendResult sendResult = rocketMQTemplate.syncSend(orderTopic, RocketMQBuilder.buildMessage(event));
        log.info("订单创建消息推送结果：{}", sendResult);
    }

}
